package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.*;
import com.sg.entity.OmsAchievement;
import com.sg.service.base.app.MOmsAchievementService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsAchievementMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAchievementServiceImpl
        extends ServiceImpl<OmsAchievementMapper, OmsAchievement>
        implements MOmsAchievementService {

    @Autowired
    OmsAchievementMapper omsAchievementMapper;

    /**
     * 3-10-01新增成果[9119]
     * gen by moon at 7/18/2024, 11:54:43 PM
     */
    @Trace(operationName = "3-10-01新增成果")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAchievement(OmsAchievement omsAchievement) {
        omsAchievement.setAchievementId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAchievement.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAchievement.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievement != null && omsAchievement.getSubjectLifeCycle() != null && omsAchievement.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievement.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsAchievement.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAchievement.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievement != null && omsAchievement.getSubjectLifeCycle() != null && omsAchievement.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievement.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAchievement.setIsValid("FALSE");
        if (super.save(omsAchievement)) {
            return omsAchievement.getAchievementId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-10-01修改成果[9121]
     * gen by moon at 4/3/2024, 6:43:08 PM
     */
    @Trace(operationName = "3-10-01修改成果")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAchievement(OmsAchievement omsAchievement) {
        omsAchievement.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievement != null && omsAchievement.getSubjectLifeCycle() != null && omsAchievement.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievement.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsAchievement != null && omsAchievement.getSubjectLifeCycle() != null && omsAchievement.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievement.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsAchievement.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievement != null && omsAchievement.getSubjectLifeCycle() != null && omsAchievement.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievement.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsAchievement != null && omsAchievement.getSubjectLifeCycle() != null && omsAchievement.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievement.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsAchievement);
    }

    /**
     * 3-10-01删除成果[9122]
     * gen by moon at 4/3/2024, 6:43:09 PM
     */
    @Trace(operationName = "3-10-01删除成果")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAchievement(String achievementId) {

        return super.removeById(achievementId);
    }

    /**
     * 3-10-01查询成果列表[9123]
     * gen by moon at 4/3/2024, 6:43:12 PM
     */
    @Trace(operationName = "3-10-01查询成果列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievement> queryAchievementList(QueryAchievementListReq reqDto) {

        try {
            QueryWrapper<OmsAchievement> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievement.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievement.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievement.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsAchievement>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-01查询成果详情[9124]
     * gen by moon at 4/3/2024, 6:43:16 PM
     */
    @Trace(operationName = "3-10-01查询成果详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsAchievement queryAchievementDetail(QueryAchievementDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsAchievement> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsAchievement> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsAchievement retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-01批量查询成果By授予及有效时间[9260]
     * gen by moon at 4/4/2024, 1:23:03 PM
     */
    @Trace(operationName = "3-10-01批量查询成果By授予及有效时间")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievement> batchQueryAchievementByAwardedEndTime(BatchQueryAchievementByAwardedEndTimeReq reqDto) {

        try {
            QueryWrapper<OmsAchievement> queryWrapper = new QueryWrapper<>();

            if (reqDto.getAchievementList() == null || reqDto.getAchievementList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("achievement_id", reqDto.getAchievementList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievement.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievement.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievement.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsAchievement>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-01批量查询成果[9260]
     * gen by moon at 7/24/2024, 2:57:35 AM
     */
    @Trace(operationName = "3-10-01批量查询成果")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievement> batchQueryAchievement(BatchQueryAchievementReq reqDto) {

        try {
            QueryWrapper<OmsAchievement> queryWrapper = new QueryWrapper<>();

            if (reqDto.getAchievementList() == null || reqDto.getAchievementList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("achievement_id", reqDto.getAchievementList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievement.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievement.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievement.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsAchievement>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-01批量查询成果by创建人(公共)[10097]
     * gen by moon at 8/15/2024, 10:39:38 PM
     */
    @Trace(operationName = "3-10-01批量查询成果by创建人(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievement> batchQueryAchievementByCreateInductionIdCom(BatchQueryAchievementByCreateInductionIdComReq reqDto) {

        try {
            QueryWrapper<OmsAchievement> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getAchievementList() == null || reqDto.getAchievementList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("create_induction_id", reqDto.getAchievementList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievement.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievement.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievement.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsAchievement>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
